# Replication Archive for: Aggarwal, Minali, Jennifer Allen, Alex Coppock, 
# Dan Frankowski, Sol Messing, Kelly Zhang, James Barnes, Andrew Beasley, 
# Harry Hantman, and Sylvan Zheng: The impact of digital campaign advertising 
# during the 2020 US presidential election: evidence from survey experiments, 
# field experiments, and a campaign-level experiment. 
# Nature Human Behavior, forthcoming.

library(tidyverse)
library(lubridate)
library(RColorBrewer)
library(patchwork)
library(scales)

df_funding <- 
  read_csv("weekly_adds_for_page_id_disclaimer_041322.csv")

df_funding <-
  df_funding %>%
  mutate(
    from_date = floor_date(from_date, unit = "month"),
    Sponsor = ifelse(str_detect(tolower(disclaimer), "acronym"), "ACRONYM", "Other")
  )

aggregated_funding <-
  df_funding %>%
  group_by(from_date) %>%
  summarize(spend = sum(new_spend))

aggregated_funding_Acronym <-
  df_funding %>%
  filter(Sponsor == "ACRONYM") %>% 
  group_by(from_date) %>%
  summarize(spend = sum(new_spend))


gg_df_1 <- 
  bind_rows(`Spending from all Sponsors` = aggregated_funding,
            `Spending from Acronym` = aggregated_funding_Acronym,
            .id = "sponsor")


g1 <- 
  ggplot(gg_df_1, aes(x = from_date, y = spend)) +
  geom_line() +
  geom_point() +
  ylab("Facebook spending from\nWesleyan Media Project") +
  xlab("Date") +
  theme_bw() +
  theme(axis.title.x = element_blank()) +
  scale_y_continuous(labels=scales::dollar_format()) +
  scale_x_date(labels=scales::date_format(format = "%B %Y")) +
  facet_wrap(~sponsor, scales = "free_y")


gg_df_2 <- 
  aggregated_funding %>% 
  left_join(rename(aggregated_funding_Acronym, acronym_spend = spend)) %>% 
  mutate(fraction = acronym_spend / spend)


g2 <- 
  ggplot(gg_df_2, aes(x = from_date, y = fraction)) +
  geom_line() +
  geom_point() +
    scale_y_continuous(breaks = seq(0, 0.0125, length.out = 6),
                       limits = c(0, 0.0125),
                       labels = percent_format()(seq(0, 0.0125, length.out = 6))) +
  ylab("Acronym share of federal\npolitical advertising on Facebook") +
  xlab("Date") +
    theme_bw() +
    theme(axis.title.x = element_blank(),
          panel.grid.minor =element_blank()) +
    scale_x_date(labels=scales::date_format(format = "%B %Y"))


g <- g1 / g2

# ggsave(
#   "output/figure_C4.pdf",
#   g,
#   height = 7,
#   width = 8
#   ) 

